X-Git-Url: https://git.r.bdr.sh/rbdr/super-polarity/blobdiff_plain/8534e46e400268c5ceffb3b14f02cef39eedae8f..3de51c6f55d304f038df1b77c8ab346e2a187fe1:/Super%20Polarity/ScoreScreen.cs diff --git a/Super Polarity/ScoreScreen.cs b/Super Polarity/ScoreScreen.cs new file mode 100644 index 0000000..bf43275 --- /dev/null +++ b/Super Polarity/ScoreScreen.cs @@ -0,0 +1,108 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.IO; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using Microsoft.Xna.Framework.Input; + +namespace SuperPolarity +{ + class ScoreScreen : Screen + { + protected SpriteFont Font; + protected NameChooserWidget nameChooser; + protected Dictionary Scores; + + public ScoreScreen(SuperPolarity newGame) : base(newGame) { } + + public override void Initialize() + { + base.Initialize(); + nameChooser = new NameChooserWidget(Game, new Vector2(40, Game.GraphicsDevice.Viewport.Height / 4)); + InputController.RegisterEventForButton("OK", Buttons.A); + InputController.RegisterEventForKey("OK", Keys.Z); + Scores = new Dictionary(); + ReadScore(); + } + + public override void LoadContent() + { + base.LoadContent(); + Font = Game.Content.Load("Fonts\\bigfont"); + InputController.Bind("OK", HandleNext); + } + + public void HandleNext(float value) + { + if (!Active) { return; } + WriteScore(); + ScreenManager.Pop(); + } + + public void WriteScore() + { + using (StreamWriter swriter = new StreamWriter("scores.txt", true)) + { + swriter.WriteLine(nameChooser.Value() + "," + Game.Player.Score.ToString()); + } + } + + public void ReadScore() + { + try + { + using (StreamReader sreader = new StreamReader("scores.txt")) + { + string line = null; + while ((line = sreader.ReadLine()) != null) + { + string[] parts = line.Split(','); + Scores.Add(parts[0], int.Parse(parts[1])); + } + } + } + catch (FileNotFoundException) + { + } + } + + public override void CleanUp() + { + base.CleanUp(); + Font = null; + } + + public override void Draw(SpriteBatch spriteBatch) + { + base.Draw(spriteBatch); + spriteBatch.DrawString(Font, "YOUR SCORE WAS: " + Game.Player.Score.ToString(), new Vector2(40, Game.GraphicsDevice.Viewport.Height / 2), Color.Black); + spriteBatch.DrawString(Font, "Use Left Stick or Arrows to Choose Name Press A when done", new Vector2(40, Game.GraphicsDevice.Viewport.Height / 2 + 40), new Color(0, 0, 0, 128)); + nameChooser.Draw(spriteBatch); + DrawScores(spriteBatch); + } + + public override void Update(GameTime gameTime) + { + base.Update(gameTime); + InputController.UpdateInput(false); + nameChooser.Update(gameTime); + } + + protected void DrawScores(SpriteBatch spriteBatch) + { + var sortedDict = (from entry in Scores orderby entry.Value descending select entry) + .Take(5) + .ToDictionary(pair => pair.Key, pair => pair.Value); + + var i = 0; + + foreach (KeyValuePair entry in sortedDict) { + i++; + spriteBatch.DrawString(Font, entry.Key + " " + entry.Value, new Vector2(40, Game.GraphicsDevice.Viewport.Height / 2 + 100 + (32 * i)), new Color(0, 0, 0, 64)); + } + + } + } +}